﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Hash
{
    class HashTable
    {
        List<string>[] hashTable;
        string[] words;
        const int CHAR_SIZE = 1024;

        public HashTable(string text)
        {
            words = text.Split(' ');

            var tableSize = defineTableSize();
            hashTable = new List<string>[tableSize];

            fillHashTable();
        }

        int defineTableSize()
        {
            int maxWordLen = 0;
            foreach (var word in words)
                if (word.Length > maxWordLen)
                    maxWordLen = word.Length;
            return maxWordLen * CHAR_SIZE;
        }

        void fillHashTable()
        {
            foreach (var word in words)
            {
                int key = genHashKey(word);

                if (hashTable[key] == null)
                    hashTable[key] = new List<string>();
                if (!hashTable[key].Contains(word))
                    hashTable[key].Add(word);
            }
        }
        int genHashKey(string word)
        {
            int key = 0;
            foreach (var lt in word) key += lt;
            return key;
        }

        public bool containsWord(string word)
        {
            int key = genHashKey(word);

            if (hashTable[key] != null)
                foreach (var item in hashTable[key])
                    if (item == word) return true;
            return false;
        }
    }
}
